package guoanmaker.operator.business.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import guoanmaker.operator.business.dao.OperatorProviderOrderDao;
import guoanmaker.operator.business.model.OperatorCoupons;
import guoanmaker.operator.business.model.OperatorRefuseRecord;
import guoanmaker.operator.business.model.OperatorUser;
import guoanmaker.operator.business.model.PersonalUserCoupon;
import guoanmaker.operator.business.model.ProvideOrderForm;
import guoanmaker.operator.business.model.ReturnResult;
import guoanmaker.operator.business.modelnobind.OderFormRo;
import guoanmaker.operator.business.modelnobind.ProvideOrderFormForOrder;
import guoanmaker.operator.business.repository.OperatorCouponsRepository;
import guoanmaker.operator.business.repository.OperatorRefuseRecordRepository;
import guoanmaker.operator.business.repository.OperatorUserRepository;
import guoanmaker.operator.business.repository.PersonalUserCouponRepository;
import guoanmaker.operator.business.repository.PersonalUserRepository;
import guoanmaker.operator.business.repository.ProvideCommodityRepository;
import guoanmaker.operator.business.repository.ProvideOrderFormRepository;

@Service
public class OperatorOrderService {
	
	@Autowired
	private ProvideOrderFormRepository  provideOrderFormRepository;
	
	@Autowired
	private OperatorProviderOrderDao  operatorProviderOrderDao;
	
	@Autowired
	private ProvideCommodityRepository  provideCommodityRepository;
	
	@Autowired
	private  OperatorRefuseRecordRepository  operatorRefuseRecordRepository;
	
	@Autowired
	private PersonalUserRepository  personalUserRepository;
	
	
	@Autowired
	private OperatorUserRepository   operatorUserRepository;
	
	@Autowired  
	private  HttpServletRequest request;
	@Autowired
	private OperatorCouponsRepository  operatorCouponsRepository;
	
	@Autowired
	private  PersonalUserCouponRepository  personalUserCouponRepository;

	
	/**
	 * 
	 * @param page
	 * @param size
	 * @param status 订单状态1待支付  2待接单  3服务中  4待确认   5退款处理中(用户发起退款)   6已完成   7已关闭  8退款完成  9退款处理中(商家已拒绝退款)
	 * @return
	 */
	public Map<String, Object>  selectOrderInfos(Integer  page,Integer size,String status){
	
		return operatorProviderOrderDao.findByProperties(page, size, status);
	}
	
	
	public   Map<String, Object>     queryOrderInfos(Integer  page,Integer size,String status,String serverName,String  startOrderTime,
            String endOrderTime,String startOrderAmount,String endOrderAmount,String companyName,String orderNum,String userid,String couponid,String busid){
		
		return  operatorProviderOrderDao.queryOrderInfos(page, size, status, serverName, startOrderTime, endOrderTime, startOrderAmount, endOrderAmount, companyName, orderNum,userid,couponid,busid);
	}
	public   Map<String, Object>     querymiaoOrderInfos(String id){
		
		return  operatorProviderOrderDao.querymiaoOrderInfos(id);
	}
	
	
	@Transactional
	public Workbook   exportOrderInfo(String  ids,String status){
		Workbook wb = new HSSFWorkbook();
	
		Sheet sheet = wb.createSheet();
		wb.setSheetName(0, "");
		Row header = sheet.createRow(0);
		header.createCell(0).setCellValue("订单号");	
		header.createCell(1).setCellValue("商品名称");	
		header.createCell(2).setCellValue("种类");	
		header.createCell(3).setCellValue("价格");	
		header.createCell(4).setCellValue("优惠券");	
		header.createCell(5).setCellValue("雇主");	
		header.createCell(6).setCellValue("下单时间");	
		header.createCell(7).setCellValue("状态");	
		header.createCell(8).setCellValue("服务商");	
		header.createCell(9).setCellValue("发票");	
		 String[]  pids=ids.split(",");
		 List<OderFormRo>   list= operatorProviderOrderDao.queryOrderInfosNoPage(status);
		 OderFormRo oderFormRo=new OderFormRo();
		 for (int i = 0; i < list.size(); i++) {
			 oderFormRo=list.get(i);
			 for (int j = 0; j< pids.length; j++) {
				 String id=pids[j];
				if(oderFormRo.getId().equals(id)){
					Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
					dataRow.createCell(0).setCellValue(oderFormRo.getOrderNumber()!=null?oderFormRo.getOrderNumber():"");
					dataRow.createCell(1).setCellValue(oderFormRo.getCommodityName()!=null?oderFormRo.getCommodityName():"");
					dataRow.createCell(2).setCellValue(oderFormRo.getServerName()!=null?oderFormRo.getServerName():"");
					dataRow.createCell(3).setCellValue(oderFormRo.getUnitPrice()!=null?oderFormRo.getUnitPrice():0);
					dataRow.createCell(4).setCellValue(oderFormRo.getCoupons()!=null?oderFormRo.getCoupons():"");
					dataRow.createCell(5).setCellValue(oderFormRo.getName()!=null?oderFormRo.getName():"");
					dataRow.createCell(6).setCellValue(oderFormRo.getPlaceOrderTime()!=null?oderFormRo.getPlaceOrderTime():null);
					dataRow.createCell(7).setCellValue(oderFormRo.getOrderStatus()!=null?oderFormRo.getOrderStatus():"");
					dataRow.createCell(8).setCellValue(oderFormRo.getBusinessName()!=null?oderFormRo.getBusinessName():"");
					if(oderFormRo.getInvoiceState()!=null){
						if(oderFormRo.getInvoiceState().equals("0")){
							dataRow.createCell(9).setCellValue("未开");
						}else if(oderFormRo.getInvoiceState().equals("1")){
							dataRow.createCell(9).setCellValue("已开");
						}
					}else{
						dataRow.createCell(9).setCellValue("");
					}
					
				}
			 }
		}
		return wb;
	}
	public Workbook   exportmiaoOrderInfos(String id){
		Workbook wb = new HSSFWorkbook();
		
		Sheet sheet = wb.createSheet();
		Sheet sheet1 = wb.createSheet();
		wb.setSheetName(0, "下单人数");
		Row header = sheet.createRow(0);
		header.createCell(0).setCellValue("商品名称");	
		header.createCell(1).setCellValue("商品现价");	
		header.createCell(2).setCellValue("秒杀价格");	
		header.createCell(3).setCellValue("下单时间");	
		 List<OderFormRo>  list=	 operatorProviderOrderDao.exquerymiaoOrderInfos(id);
		 OderFormRo oderFormRo=null;
		for (int i = 0; i < list.size(); i++) {
			        oderFormRo=list.get(i);
					Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
					dataRow.createCell(0).setCellValue(oderFormRo.getCommodityName()!=null?oderFormRo.getCommodityName():"");
					dataRow.createCell(1).setCellValue(oderFormRo.getTruePrice()!=null?oderFormRo.getTruePrice():0);
					dataRow.createCell(2).setCellValue(oderFormRo.getSpikePrice()!=null?oderFormRo.getSpikePrice():0);
					if(oderFormRo.getPlaceOrderTime()!=null&&!oderFormRo.getPlaceOrderTime().equals("")){
						SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss ");
				    	String ss=	sdf.format(oderFormRo.getPlaceOrderTime()); 
						dataRow.createCell(3).setCellValue(ss);
					}else{
						dataRow.createCell(3).setCellValue("");
					}
					
		}
		wb.setSheetName(1, "支付人数");
		Row header1 = sheet1.createRow(0);
		header1.createCell(0).setCellValue("商品名称");	
		header1.createCell(1).setCellValue("商品现价");	
		header1.createCell(2).setCellValue("支付价钱");	
		header1.createCell(3).setCellValue("支付时间");	
		List<OderFormRo>  list1=	 operatorProviderOrderDao.exquerymiaoOrderInfoszhifu(id);
		 OderFormRo oderFormRo1=null;
		 for (int i = 0; i < list1.size(); i++) {
			 oderFormRo1=list1.get(i);
				Row dataRow = sheet1.createRow(sheet1.getLastRowNum() + 1);
				dataRow.createCell(0).setCellValue(oderFormRo1.getCommodityName()!=null?oderFormRo1.getCommodityName():"");
				dataRow.createCell(1).setCellValue(oderFormRo1.getTruePrice()!=null?oderFormRo1.getTruePrice():0);
				dataRow.createCell(2).setCellValue(oderFormRo1.getSpikePrice()!=null?oderFormRo1.getSpikePrice():0);
				if(oderFormRo1.getDefrayTime()!=null&&!oderFormRo1.getDefrayTime().equals("")){
					SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss ");
					String ss=	sdf.format(oderFormRo1.getDefrayTime()); 
					dataRow.createCell(3).setCellValue(ss);
				}else{
					dataRow.createCell(3).setCellValue("");
				}
		}
		
		return wb;
	}
	
	
	
	@Transactional
	public  ReturnResult  closeSureOrder(String id,String refuseName,String content,Double refunds,String userid){
		    ReturnResult returnResult =new ReturnResult();
			OperatorRefuseRecord  operatorRefuseRecord=new OperatorRefuseRecord();
			if(content!=null&&!content.equals("")){
				ProvideOrderForm  provideOrderForm=	provideOrderFormRepository.findOne(id);
				if(provideOrderForm!=null){
					/*provideOrderForm.setOrderStatus("7");
					provideOrderFormRepository.save(provideOrderForm);*/
					operatorRefuseRecord.setAttributId(id);
					operatorRefuseRecord.setContent(content);
					operatorRefuseRecord.setReasonName(refuseName);
					operatorRefuseRecord.setRefuseTime(new Date());
					operatorRefuseRecord.setOpchannel(2);
					operatorRefuseRecord.setRefunds(refunds);
					operatorRefuseRecord.setStatus(4);
					OperatorUser op=operatorUserRepository.findOne(userid);
					if(op!=null){
						operatorRefuseRecord.setUseraccountforcord(op.getUsername()+"关闭");
					}
					operatorRefuseRecordRepository.save(operatorRefuseRecord);
					returnResult.setKey("success");
					returnResult.setValue("关闭成功");
				}else{
					returnResult.setKey("error");
					returnResult.setValue("关闭失败");
				}
			}else{
				returnResult.setKey("error");
				returnResult.setValue("内容不可为空");
			}
		return  returnResult;
	}
	
	public ProvideOrderFormForOrder findbyOrderId(String id){
		
		return operatorProviderOrderDao.findByid(id);
	}
	
	
	public  String selectCouponidForinfo(String couponid){
		String str="";
		PersonalUserCoupon personalUserCoupon=	personalUserCouponRepository.findOne(couponid);
		if(personalUserCoupon!=null){
			OperatorCoupons operatorCoupons=operatorCouponsRepository.findOne(personalUserCoupon.getOcId());
			if(operatorCoupons!=null){
				str=("满"+operatorCoupons.getFullamount()+"减"+operatorCoupons.getSubamount());
			}
		}
		return str;
	}
	
	
	

}
